package edu.sandau.b230111.b23011125.Service.Impl;

import edu.sandau.b230111.b23011125.Global.ContestWithProblems;
import edu.sandau.b230111.b23011125.Global.GlobalEnum;
import edu.sandau.b230111.b23011125.Global.ProblemListWithProblems;
import edu.sandau.b230111.b23011125.Model.Contest;
import edu.sandau.b230111.b23011125.Model.ProblemList;
import edu.sandau.b230111.b23011125.Model.RankList;
import edu.sandau.b230111.b23011125.Repository.ContestRepository;
import edu.sandau.b230111.b23011125.Repository.RankListRepository;
import edu.sandau.b230111.b23011125.Service.ContestService;
import edu.sandau.b230111.b23011125.Service.ProblemListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

@Service
public class ContestServiceImpl implements ContestService {
    @Autowired
    private ContestRepository contestRepository;
    @Autowired
    private RankListRepository rankListRepository;
    @Autowired
    private ProblemListService problemListService;

    @Override
    public ContestWithProblems queryContestWithProblems(String contestid) {
        ContestWithProblems contestWithProblems = new ContestWithProblems();
        Contest contest = contestRepository.getReferenceById(contestid);
        contestWithProblems.setContest(contest);
        ProblemList problemList = problemListService.queryProblemListById(contest.getProblemlistid());
        ProblemListWithProblems problemListWithProblems = problemListService.queryProblemListWithProblems(problemList.getListid());
        contestWithProblems.setProblems(problemListWithProblems.getProblems());
        return contestWithProblems;
    }

    @Override
    public RankList queryRankListById(String ranklistid) {
        return rankListRepository.getReferenceById(ranklistid);
    }

    @Override
    public Page<Contest> queryAllContest(Integer pageCnt, Integer pageSize) {
        Pageable page = PageRequest.of(pageCnt,pageSize);
        Page<Contest> contest = contestRepository.findAll(page);
        return contest;
    }

    @Override
    public Contest queryContestById(String contestid) {
        return contestRepository.getReferenceById(contestid);
    }

    @Override
    public GlobalEnum.OperateResult modifyRanklist(String contestid, String userid, String result,String count) {
        Optional<RankList> rankLists = rankListRepository.findByContestidAndUserid(contestid,userid);
//        System.out.println(1);
        RankList rankList = new RankList();
        if(!rankLists.isPresent()){
            rankList.setRanklistid(UUID.randomUUID().toString());
            rankList.setContestid(contestid);
            rankList.setUserid(userid);
            rankList.setProblemAstatus(0);
            rankList.setProblemAcost(0);
            rankList.setProblemAaddcost(0);
            rankList.setTotalcost(0);
            rankList.setRightcount(0);

            rankList.setProblemBstatus(0);
            rankList.setProblemBcost(0);
            rankList.setProblemBaddcost(0);

            rankList.setProblemCstatus(0);
            rankList.setProblemCcost(0);
            rankList.setProblemCaddcost(0);

            rankList.setProblemDstatus(0);
            rankList.setProblemDcost(0);
            rankList.setProblemDaddcost(0);

            rankList.setProblemEstatus(0);
            rankList.setProblemEcost(0);
            rankList.setProblemEaddcost(0);
            rankListRepository.save(rankList);
        }
        else{
            rankList = rankLists.get();
        }

        if(count.equals("1")){
            if(rankList.getProblemAstatus()==1){
                return GlobalEnum.OperateResult.SUCC;
            }
            if(result.equals("通过")){
                Date now = new Date();
                Date starttime = contestRepository.getReferenceById(contestid).getStarttime();
                long durtime = now.getTime() - starttime.getTime();
                long cost = durtime/1000/60;
                System.out.println(cost);
                rankList.setProblemAcost(cost);
                rankList.setProblemAstatus(1);
            }
            else{
                rankList.setProblemAstatus(2);
                rankList.setProblemAaddcost(rankList.getProblemAaddcost()+50);
            }
        }
        else if(count.equals("2")){
            if(rankList.getProblemBstatus()==1){
                return GlobalEnum.OperateResult.SUCC;
            }
            if(result.equals("通过")){
                Date now = new Date();
                Date starttime = contestRepository.getReferenceById(contestid).getStarttime();
                long durtime = now.getTime() - starttime.getTime();
                Long cost = durtime/1000/60;
                rankList.setProblemBcost(cost);
                rankList.setProblemBstatus(1);
            }
            else{
                rankList.setProblemBstatus(2);
                rankList.setProblemBaddcost(rankList.getProblemBaddcost()+50);
            }
        }
        else if(count.equals("3")){
            if(rankList.getProblemCstatus()==1){
                return GlobalEnum.OperateResult.SUCC;
            }
            if(result.equals("通过")){
                Date now = new Date();
                Date starttime = contestRepository.getReferenceById(contestid).getStarttime();
                long durtime = now.getTime() - starttime.getTime();
                Long cost = durtime/1000/60;
                rankList.setProblemCcost(cost);
                rankList.setProblemCstatus(1);
            }
            else{
                rankList.setProblemCstatus(2);
                rankList.setProblemCaddcost(rankList.getProblemCaddcost()+50);
            }
        }
        else if(count.equals("4")){
            if(rankList.getProblemDstatus()==1){
                return GlobalEnum.OperateResult.SUCC;
            }
            if(result.equals("通过")){
                Date now = new Date();
                Date starttime = contestRepository.getReferenceById(contestid).getStarttime();
                long durtime = now.getTime() - starttime.getTime();
                long cost= durtime/1000/60;
                rankList.setProblemDcost(cost);
                rankList.setProblemDstatus(1);
            }
            else{
                rankList.setProblemDstatus(2);
                rankList.setProblemDaddcost(rankList.getProblemDaddcost()+50);
            }
        }else{
            if(rankList.getProblemEstatus()==1){
                return GlobalEnum.OperateResult.SUCC;
            }
            if(result.equals("通过")){
                Date now = new Date();
                Date starttime = contestRepository.getReferenceById(contestid).getStarttime();
                long durtime = now.getTime() - starttime.getTime();
                long cost = durtime/1000/60;
                rankList.setProblemEcost(cost);
                rankList.setProblemEstatus(1);
            }
            else{
                rankList.setProblemEstatus(2);
                rankList.setProblemEaddcost(rankList.getProblemEaddcost()+50);
            }
        }
        rankListRepository.save(rankList);
        return GlobalEnum.OperateResult.SUCC;
    }

    @Override
    public Page<RankList> queryAllRankList(String contestid,Integer pageCnt, Integer pageSize) {
        Pageable page = PageRequest.of(pageCnt,pageSize, Sort.by("rightcount").descending().and(Sort.by("totalcost")));
        return rankListRepository.findByContestid(contestid,page);
    }
}
